library(tidyverse)
library(readstata13)

## set working directory to Dataverse replicaiton
## folder

survey <- read.dta13("Harris_Data/Harris 1976 Economic Survey, study no. 7684/harris_s7684_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(7684)

# study year (year)
survey$year <- 1976

# geographic data (urban)
summary(survey$S13)
survey$urban <- as.character(survey$S13)
table(survey$urban)

# geographic data (region)
survey$region <- as.character(survey$S11)
table(survey$region)

# respondent head of household (hh)
survey$hh <- as.character(survey$F1)
table(survey$hh)

# increasing inequality (inequality) cannot find variable
summary(survey$Q10_2)
survey$inequality <- as.character(survey$Q10_2)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 4

# union (union.self)
survey$union.self <- as.character(survey$F6_1)
survey$union.self[survey$F6_4 == "Yes"] <- "Not Sure"
table(survey$union.self)

survey$union.other <- as.character(survey$F6_2)
survey$union.other[survey$F6_4 == "Yes"] <- "Not Sure"
table(survey$union.other)

# employment (employed)
table(survey$F2A)
survey$employed <- as.character(survey$F2A)
table(survey$employed)

## empl self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F2B)
table(survey$occupation)

## occ self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
table(survey$F5)
survey$educ <- as.character(survey$F5)                        

# household income (income)
table(survey$F7)
survey$income <- as.character(survey$F7)

# age
table(survey$F4)
survey$age <- as.character(survey$F4)

# race
table(survey$F9)
survey$race <- as.character(survey$F9)

# politics (party)
table(survey$P1C)
survey$party <- as.character(survey$P1C)
table(survey$party)

# politics (ideology)
table(survey$P1K)
survey$ideology <- as.character(survey$P1K)

# gender
summary(survey$F10)
survey$gender <- as.character(survey$F10)

# religion
survey$religion <- as.character(survey$F8)
table(survey$religion)

## factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- NA
survey$dontcount <- NA
survey$leftout <- NA

## question_place
survey$question_place <- "after party"

# subset
survey_7684 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                         "inequality", "inequality.variable", "union.self", "union.other",
                         "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                         "age", "race", "party", "ideology", "gender", "religion",
                         "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                         "question_place")]

# save file
#saveRDS(survey_7684, file = "Harris_Data/survey_7684.rds")
